<template>
    <div class="e-form">
        <BillTop title="设备调动变更新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.handoverTransfer" label-width="200px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="调动编号：">
                            <span>{{ formData.handoverTransfer.billNo }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="填报日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.handoverTransfer.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="经办人：" prop="agenter">
                            <el-input v-model="formData.handoverTransfer.agenter" readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectHandling"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                         <el-form-item label="调入机构：">
                            <span>{{formData.handoverTransfer.transferOrg}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="接收人：" prop="receiver">
                            <el-input v-model="formData.handoverTransfer.receiver" readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectReceiver"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                            <span>{{ statusLable(formData.handoverTransfer.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="租赁金额（不含税）：">
                             <span>{{ formData.handoverTransfer.amount }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="租赁金额（含税）：">
                             <span>{{ formData.handoverTransfer.leaseAmount }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="税率%：">
                            <el-input v-model="formData.handoverTransfer.tax"
                                @blur="taxBlur"
                                v-enter-integer2="{
                                    set:formData,
                                    val:'handoverTransfer',
                                    key:'tax'
                                }">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="税额（元）：">
                            <el-input v-model="formData.handoverTransfer.taxAmount"
                            :disabled="isDisabled"
                            v-enter-float="{
                                set:formData,
                                val:'handoverTransfer',
                                key:'taxAmount'
                            }">
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                     <el-col :span="24">
                        <el-form-item label="机构：">
                            <span>{{formData.handoverTransfer.orgName}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item  label="备注信息：">
                        <el-input type="textarea" v-model="formData.handoverTransfer.remarks" placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose" v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import '@/utils/jquery.scrollTo.min'
import request from '@/api/equip/purchaseAcceptance.js'
import { selectName, selectReceiver } from '@/api/equip/popupCallback.js'
import count from '@/api/equip/equipHandoverCount.js'
export default {
    data () {
        return {
            formData: {
                //基础信息
                'handoverTransfer': {
                    'agenter': '',
                    'amount': null,
                    'auditDate': '',
                    'auditor': '',
                    'auditorId': '',
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'changeBillId': '',
                    'changeBillNo': '',
                    'changeState': 0,
                    'easId': '',
                    'founderId': '',
                    'founderName': '',
                    'gmtCreate': '',
                    'gmtModified': '',
                    'leaseAmount': null,
                    'modifier': '',
                    'modifierId': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDescription': '',
                    'nullifyReason': '',
                    'number': null,
                    'orgId': '',
                    'orgName': '',
                    'receiver': '',
                    'recorder': '',
                    'recorderId': '',
                    'remarks': '',
                    'state': null,
                    'tax': null,
                    'taxAmount': null,
                    'transferOrg': '',
                    'transferOrgId': '',
                    'version': null,
                    'workId': ''
                }
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择填报日期', trigger: 'change' },
                ]
            },
            isDisabled: false,
            billid: this.$route.query.billid,
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            currencyForm: state => state.equip.equipData.currencyForm//币种
        }),
        //状态
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>+x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
    },
    components: {
    },
    created () {
        this.userId = localStorage.getItem('userId')
        this.getInfo()
    },
    mounted () {

    },
    methods: {
        //基本信息查询
        getInfo () {
            const date = new Date()
            const params = {
                id: this.billid,
            }
            request.equipMoveInitBaseInfo(params).then(res=>{
                this.formData.handoverTransfer = res.handoverTransfer
                this.isDisabled  = this.formData.handoverTransfer.tax == 0 ? true : false
                this.formData.handoverTransfer.state = 0
                this.formData.handoverTransfer.changeBillId = this.billid
                this.formData.handoverTransfer.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            })
        },
        //选择经办人 (选择本机构用户)
        selectHandling () {
            selectName().then(res=>{
                this.formData.handoverTransfer.agenter = res[0].userName
            })
        },
        //选择接入人
        selectReceiver () {
            if(!this.formData.handoverTransfer.transferOrg) {
                this.clientPop('err', '请先选择调入机构！')
                return
            }else{
                selectReceiver(this.formData.handoverTransfer.transferOrgId).then(res=>{
                    this.formData.handoverTransfer.receiver = res[0].userName
                    this.formData.handoverTransfer.recorderId = res[0].userId
                })
            }
        },
        /**税额 = 租赁金额（不含税）*税率 */
        taxBlur () {
            this.isDisabled  = this.formData.handoverTransfer.tax == 0 ? true : false
            this.formData.handoverTransfer.taxAmount = count.onTaxAmount (+this.formData.handoverTransfer.amount, +this.formData.handoverTransfer.tax)
            this.onLeaseAmount()
        },
        //租赁金额(含税) = 租赁金额（不含税）+税额
        onLeaseAmount () {
            this.formData.handoverTransfer.leaseAmount = count.onLeaseAmount(+this.formData.handoverTransfer.amount, +this.formData.handoverTransfer.taxAmount)
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if (valid) {
                    request.equipMoveChangeAdd(this.formData.handoverTransfer).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/equipMoveChangeEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                } else {
                    return false
                }
            })
        },
        handleClose () {
            this.$router.replace('/equipMoveList')
        },
        onChangeTab (e) {
            console.log('clickTabFlag: ', this.clickTabFlag)
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
        },

    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.form {
    margin-top: 10px;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
